package org.jeecg.modules.wms.warehouse.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.modules.wms.config.WarehouseDictEnum;
import org.jeecg.modules.wms.warehouse.entity.WmsStorageLocations;
import org.jeecg.modules.wms.warehouse.entity.WmsStorageZones;
import org.jeecg.modules.wms.warehouse.mapper.WmsStorageLocationsMapper;
import org.jeecg.modules.wms.warehouse.service.IWmsStorageLocationsService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * @Description: 储位表
 * @Author: jeecg-boot
 * @Date:   2025-10-25
 * @Version: V1.0
 */
@Service
public class WmsStorageLocationsServiceImpl extends ServiceImpl<WmsStorageLocationsMapper, WmsStorageLocations> implements IWmsStorageLocationsService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void enable(String id) {
        boolean empty = ObjectUtil.isEmpty(id);
        if (empty) {
            throw new RuntimeException("参数不能为空");
        }
        WmsStorageLocations storageLocations = this.getOne(Wrappers.lambdaQuery(WmsStorageLocations.class).eq(WmsStorageLocations::getId, id));
        if (ObjectUtil.isNull(storageLocations)) {
            throw new RuntimeException("仓库不存在");
        }
        if (!WarehouseDictEnum.STATUS_CREATED.getCode().equals(storageLocations.getStatus())
                && !WarehouseDictEnum.STATUS_INACTIVE.getCode().equals(storageLocations.getStatus())) {
            throw new RuntimeException("仓库状态异常,不能被启用");
        }
        storageLocations.setStatus(WarehouseDictEnum.STATUS_ACTIVE.getCode());
        this.updateById(storageLocations);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void disable(String id) {
        boolean empty = ObjectUtil.isEmpty(id);
        if (empty) {
            throw new RuntimeException("参数不能为空");
        }
        WmsStorageLocations storageLocations = this.getOne(Wrappers.lambdaQuery(WmsStorageLocations.class).eq(WmsStorageLocations::getId, id));
        if (ObjectUtil.isNull(storageLocations)) {
            throw new RuntimeException("仓库不存在");
        }
        if (!WarehouseDictEnum.STATUS_ACTIVE.getCode().equals(storageLocations.getStatus())) {
            throw new RuntimeException("仓库状态异常,不能被禁用");
        }
        storageLocations.setStatus(WarehouseDictEnum.STATUS_INACTIVE.getCode());
        this.updateById(storageLocations);
    }

    @Transactional (rollbackFor = {Exception.class})
    @Override
    public void edit(WmsStorageLocations wmsStorageLocations) {
        if (wmsStorageLocations.getStatus().equals(WarehouseDictEnum.STATUS_ACTIVE.getCode())) {
            WmsStorageLocations storageLocations = this.getOne(Wrappers.lambdaQuery(WmsStorageLocations.class).eq(WmsStorageLocations::getId, wmsStorageLocations.getId()));
            if (ObjectUtil.isEmpty(storageLocations)) {
                throw new RuntimeException("储位不存在");
            }
            Map<String, Object> oldMap = BeanUtil.beanToMap(wmsStorageLocations);
            Map<String, Object> newMap = BeanUtil.beanToMap(storageLocations);
            String[] criticalFields = {"locationCode", "locationCategory", "locationType", "status", "warehouseId", "areaId"};
            for (String field : criticalFields) {
                if (!ObjectUtil.equal(oldMap.get(field), newMap.get(field))) {
                    throw new RuntimeException("储位状态异常");
                }
            }
        }
        this.updateById(wmsStorageLocations);
    }
}
